package com.zf.zfed.audit.ui

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.zf.zfed.audit.R

/**
 * 指纹管理卡片组件
 */
class FingerprintManagementCard @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    
    private var fingerprintIcon: ImageView
    private var fingerprintTitle: TextView
    private var fingerprintStatus: TextView
    private var actionIcon: ImageView
    
    init {
        orientation = HORIZONTAL
        isClickable = true
        isFocusable = true
        
        // 设置卡片背景
        background = ContextCompat.getDrawable(context, R.drawable.card_background)
        
        // 设置卡片边距
        val cardPadding = context.dpFromDimen(R.dimen.spacing_8)
        setPadding(cardPadding, cardPadding, cardPadding, cardPadding)
        
        // 左侧图标和文字
        val leftContainer = FrameLayout(context)
        val leftLP = LayoutParams(0, LayoutParams.MATCH_PARENT, 1f)
        addView(leftContainer, leftLP)
        
        // 指纹图标
        fingerprintIcon = ImageView(context)
        val iconSize = context.dpFromDimen(R.dimen.size_22)
        val iconLP = FrameLayout.LayoutParams(iconSize, iconSize)
        iconLP.gravity = Gravity.START or Gravity.CENTER_VERTICAL
        iconLP.leftMargin = context.dpFromDimen(R.dimen.spacing_8)
        fingerprintIcon.setImageResource(R.drawable.u3228)
        fingerprintIcon.setColorFilter(ContextCompat.getColor(context, R.color.text_primary))
        leftContainer.addView(fingerprintIcon, iconLP)
        
        // 指纹标题
        fingerprintTitle = TextView(context)
        fingerprintTitle.setTextColor(ContextCompat.getColor(context, R.color.text_primary))
        fingerprintTitle.textSize = context.spFromDimen(R.dimen.font_16)
        fingerprintTitle.setTypeface(null, android.graphics.Typeface.BOLD)
        val titleLP = FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        titleLP.gravity = Gravity.START or Gravity.CENTER_VERTICAL
        titleLP.leftMargin = context.dpFromDimen(R.dimen.spacing_40)
        leftContainer.addView(fingerprintTitle, titleLP)
        
        // 右侧状态和操作
        val rightContainer = LinearLayout(context)
        rightContainer.orientation = HORIZONTAL
        rightContainer.gravity = Gravity.CENTER_VERTICAL
        val rightLP = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
        rightLP.gravity =  Gravity.CENTER_VERTICAL
        addView(rightContainer, rightLP)
        
        // 指纹状态
        fingerprintStatus = TextView(context)
        fingerprintStatus.setTextColor(ContextCompat.getColor(context, R.color.text_primary))
        fingerprintStatus.textSize = context.spFromDimen(R.dimen.font_18)
        val statusLP = LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        statusLP.rightMargin = context.dpFromDimen(R.dimen.spacing_8)
        rightContainer.addView(fingerprintStatus, statusLP)
        
        // 操作图标
        actionIcon = ImageView(context)
        val actionIconSize = context.dpFromDimen(R.dimen.size_30)
        val actionIconLP = LinearLayout.LayoutParams(actionIconSize, actionIconSize)
        actionIcon.setColorFilter(ContextCompat.getColor(context, R.color.text_primary))
        rightContainer.addView(actionIcon, actionIconLP)
        
        if (attrs != null) {
            val typedArray = context.obtainStyledAttributes(attrs, R.styleable.FingerprintManagementCard)
            val title = typedArray.getString(R.styleable.FingerprintManagementCard_fingerprint_title)
            val status = typedArray.getString(R.styleable.FingerprintManagementCard_fingerprint_status)
            val actionIconRes = typedArray.getResourceId(R.styleable.FingerprintManagementCard_fingerprint_action_icon, 0)
            
            fingerprintTitle.text = title ?: "指纹"
            fingerprintStatus.text = status ?: "未录指纹"
            
            if (actionIconRes != 0) {
                actionIcon.setImageResource(actionIconRes)
            }
            
            typedArray.recycle()
        }
    }
    
    /**
     * 设置指纹标题
     */
    fun setFingerprintTitle(title: String) {
        fingerprintTitle.text = title
    }
    
    /**
     * 设置指纹状态
     */
    fun setFingerprintStatus(status: String) {
        fingerprintStatus.text = status
    }
    
    /**
     * 设置操作图标
     */
    fun setActionIcon(iconRes: Int) {
        actionIcon.setImageResource(iconRes)
    }
    
    /**
     * 设置点击监听器
     */
    fun setOnActionClickListener(listener: () -> Unit) {
        setOnClickListener { listener() }
    }
    
    /**
     * 获取指纹状态
     */
    fun getFingerprintStatus(): String {
        return fingerprintStatus.text.toString()
    }
}
